package com.atguigu.ssyx.acl.controller;

import com.atguigu.ssyx.acl.service.RoleService;
import com.atguigu.ssyx.acl.service.impl.RoleServiceImpl;
import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.model.acl.Role;
import com.atguigu.ssyx.vo.acl.RoleQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "角色管理")
@RestController
@RequestMapping("/admin/acl/role")
@CrossOrigin //跨域问题
public class RoleController {
    //    注入service
    @Autowired
    private RoleService roleService;

    //    1.角色列表(条件分页查询)
    @ApiOperation("角色列表")
    @GetMapping("/{current}/{limit}")
    public Result pageList(
            @ApiParam(value = "当前页码", name = "current", required = true)
            @PathVariable Long current,
            @ApiParam(value = "每页记录数", name = "limit", required = true)
            @PathVariable Long limit,
            RoleQueryVo roleQueryVo
    ) {
//        1 创建page对象,传递当前页和每页记录数
//        current 当前页  limit 每页显示的记录数
        Page<Role> pageParam = new Page<>(current, limit);
//        2 调用service方法实现条件分页查询,返回分页对象
        IPage<Role> pageModel = roleService.selectRolePage(pageParam, roleQueryVo);
        return Result.ok(pageModel);
    }

    //    2.根据ID查询角色
    @ApiOperation("根据id查询角色")
    @GetMapping("/get/{id}")
    public Result get(@PathVariable Long id) {
        Role role = roleService.getById(id);
        return Result.ok(role);
    }

    //    3.添加角色
    @ApiOperation("添加角色")
    @PostMapping("/save")
    public Result save(@RequestBody Role role) {
        if (roleService.save(role)) {
            return Result.ok("添加成功");
        }
        return Result.fail("添加失败");
    }

    //    4.修改角色
    @ApiOperation("修改角色")
    @PutMapping("/update")
    public Result update(@RequestBody Role role) {
        if (roleService.updateById(role)) {
            return Result.ok("更新成功");
        }
        return Result.fail("更新失败");
    }

    //    5.根据ID删除角色
    @ApiOperation("根据ID删除角色")
    @DeleteMapping("/remove/{id}")
    public Result deleteById(@PathVariable Long id) {
        if (roleService.removeById(id)) {
            return Result.ok("删除成功");
        }
        return Result.fail("删除失败");
    }

    //    6.批量删除角色
    @ApiOperation("批量删除角色")
    @DeleteMapping("/batchRemove")
    public Result deleteAllRole(@RequestBody List<Long> ids) {
        if (roleService.removeByIds(ids)) {
            return Result.ok("删除完毕");
        }
        return Result.fail("删除失败");
    }

}
